﻿using Morin.Services;
using Morin.Shared.Models;
using Stylet;
using StyletIoC;
using System.Reflection;

namespace Morin.Wpf.ViewModels.Settings;

public class SettingsViewModel(IContainer container, IAppService appService) : Conductor<Screen>
{
    private MenuModel? _menuItem;
    public MenuModel? MenuItem
    {
        get => _menuItem;
        set
        {
            _menuItem = value;
            Navigate(value);
        }
    }

    public BindableCollection<MenuModel>? Menus { get; set; }

    protected override void OnInitialActivate()
    {
        base.OnInitialActivate();
        var menus = appService.GetMenus().ToList();
        var menu = menus.FirstOrDefault(x => x.Title is "设置");
        if (menu != null) Menus = [.. menus.Where(x => x.Pid == menu.Id && x.IsVisible == true)];
    }


    private void Navigate(MenuModel? menuBar)
    {
        var x = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(x => x is { IsClass: true, IsAbstract: false } && x.Name.Equals(menuBar?.ViewModel));
        if (x != null) ActivateItem((Screen)container.Get(x));
    }
}